package cn.th.service.impl;

import cn.th.entity.Admin;
import cn.th.entity.Advertising;
import cn.th.mapper.AdminMapper;
import cn.th.service.IAdminService;
import cn.th.utils.JwtUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private AdminMapper adminMapper;
    /**
     * 管理员登陆验证账号密码和状态
     * @param admin1
     * @return
     */
    @Override
    public Map<String, Object> login(Admin admin1) {

        //结果不为空，则生成token，并将用户信息存入redis
        if (admin1 != null){
            //暂时用uuid，终极方案是jwt
            //String key = "admin-" + UUID.randomUUID();

            //存入redis
            admin1.setPassword(null);
            //redisTemplate.opsForValue().set(key, admin1, 30, TimeUnit.MINUTES);

            // 创建jwt
            String token = jwtUtil.createToken(admin1);
            //返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            return data;
        }
        return null;
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @Override
    public Map<String, Object> getUserInfo(String token) {
        //jwtUtil.createToken(token);
        //根据token获取用户信息，redis
        //Object obj = redisTemplate.opsForValue().get(token);
        Admin admin ;
        try {
            admin = jwtUtil.parseToken(token, Admin.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (admin != null){
            //Admin admin = JSON.parseObject(JSON.toJSONString(obj),Admin.class);
            //存放数据 例如名字 头像
            Map<String, Object> data = new HashMap<>();
            data.put("roles",admin.getUsername());//用户名
            data.put("name",admin.getName());//名字
            data.put("avatar",admin.getAvatar());//头像
            data.put("introduction","myName："+admin.getName());//自我介绍
            return data;
        }
        return null;
    }

    /**
     * 退出登录并删除token
     * @param token
     */
    @Override
    public void logout(String token) {
        //redisTemplate.delete(token);
    }

    /**
     * 管理员查询
     * @param page
     * @param pageSize
     * @param name
     * @param school
     * @return
     */
    @Override
    public Map<String, Object> queryAd(int page, int pageSize, String name, String username, String school) {
        log.info("管理员名字为：{}，学校为：{}", name, school);
        //构造分页构造器
        Page<Admin> page1 = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        wrapper.eq(StringUtils.hasLength(name), Admin::getName, name);
        wrapper.eq(StringUtils.hasLength(school), Admin::getSchool, school);
        wrapper.eq(StringUtils.hasLength(username), Admin::getUsername, username);
        //添加排序条件
        wrapper.orderByDesc(Admin::getUpdateTime);
        //执行查询
        adminService.page(page1,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total",page1.getTotal());
        data.put("rows",page1.getRecords());

        return data;
    }

    /**
     * 删除管理员
     * @param ids
     */
    @Transactional
    public void removeBatchByIds(String ids) {
        adminMapper.deleteBatchByIds(ids);
    }


}
